﻿using S2.Models;
using S2.Services;
using S2.Services.Models;
using System.Web.Http;

namespace S2.Controllers
{
    public class IdentityController : ApiController
    {
        // GET: /api/Users api/{controller}/{id}
        // /api/identity/api/register
        [HttpPost]
        [Route("api/register")]
        public AppResult Register(User user)
        {
            using (var service = new UserService())
            {
                var products = service.Add(user);
            }
            return new AppResult();
        }
        [HttpPost]
        [Route("api/login")]
        public AppResult<bool> Login(string email, string password)
        {
            using (var service = new UserService())
            {
                var user = service.GetUserByEmailAndPassword(email, password);
                if (user == null) return new AppResult<bool>(false, "用户名或密码错误");
                return new AppResult<bool>(true, "登录成功");
            }
        }
        [HttpPost]
        [Route("api/forgot-password")]
        public AppResult<string> ForgotPassword(string email)
        {
            using (var service = new UserService())
            {
                var safeQuestion = service.GetSafeQuestion(email);
                return new AppResult<string>(safeQuestion);
            }
        }

        [HttpPost]
        [Route("api/reset-password")]
        public AppResult ResetPassword(ResetPasswordInput input)
        {
            using (var service = new UserService())
            {
                var safeQuestion = service.RestPassword(input);
                return new AppResult();
            }
        }
    }
}
